python waitKey

2 大幅改进

2012-11-16 22:07:10 应该算是毕包吧?

import threading
import time

def waitKey(t):
    '''cloure'''
    class GetKey(threading.Thread):
        ''''''
        def __init__(self):
            threading.Thread.__init__(self)
            #self.lock = threading.Lock()
            self.setDaemon(True)

        def run(self):
            waitKey.strin = raw_input()

    def get_key(t):
        GetKey().start()
        time.sleep(t)

    waitKey.strin = ""
    thrd = get_key(t/1000.0)
    return waitKey.strin

if __name__ == "__main__":
    print waitKey(3000)

1

看 java 对于 python 也有帮助,特别是根据java写的 threading 库 终于写出来了早就想写的一个函数 使用:

form waitKey import waitKey
waitKey(3000)   // 延时3s,等待输入

函数:

import threading
import time

class GetKey(threading.Thread):
    ''''''
    def __init__(self):
        threading.Thread.__init__(self)
        self.lock = threading.Lock()
        self.setDaemon(True)

    def run(self):
        global strin
        strin = raw_input()

class WaitKey(threading.Thread):
    ''''''
    def __init__(self,t):
        self.t = t
        threading.Thread.__init__(self)

    def run(self):
        GetKey().start()
        time.sleep(self.t)

def waitKey(t):
    global strin
    strin = ""
    thrd = WaitKey(t/1000.0)
    thrd.start()
    thrd.join()
    return strin

if __name__ == "__main__":
    print waitKey(3000)